package com.xian.ucenter.controller.admin;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xian.ucenter.commons.Result;
import com.xian.ucenter.entities.Post;
import com.xian.ucenter.entities.PostTag;
import com.xian.ucenter.entities.Tag;
import com.xian.ucenter.entities.vo.PostQuery;
import com.xian.ucenter.entities.vo.TagPostCount;
import com.xian.ucenter.entities.vo.TagQuery;
import com.xian.ucenter.service.PostTagService;
import com.xian.ucenter.service.TagService;
import com.xian.ucenter.service.impl.TagServiceImpl;
import com.xian.ucenter.utils.PageUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 标签管理表 前端控制器
 * </p>
 *
 * @author xian
 * @since 2022/07/23 02:59
 */
@RestController
@RequestMapping("/ucenter/admin/tag")
public class TagController {
    @Autowired
    private TagServiceImpl tagService;
    @Autowired
    private PostTagService postTagService;
    @Resource
    private PageUtil<Tag> tagPageUtil;

    @PostMapping("/save")
    public Result save (@RequestBody Tag tag) {
        return tagService.updateById(tag)?Result.ok():Result.fail();
    }

    @PostMapping("/create")
    public Result create (@RequestBody Tag tag) {
        return tagService.save(tag) ? Result.ok():Result.fail();
    }

    @GetMapping("/{tagId}")
    public Result getTag (@PathVariable("tagId") String tagId) {
        Tag tag = tagService.getById(tagId);
        if (ObjectUtil.isNull(tag))
            throw new RuntimeException("标签不存在");
        return Result.ok("tag",tag);
    }

    @PostMapping("/search/{current}/{count}")
    public Result getTag (@PathVariable("current") Integer current, @PathVariable("count") Integer count, @RequestBody(required = false) TagQuery tagQuery) {
        QueryWrapper<Tag> tagQueryWrapper = new QueryWrapper<>();
        if (ObjectUtil.isNotNull(tagQuery)) {
            String tagName = tagQuery.getTagName();
            Date begin = tagQuery.getCreateTime();
            Date end = tagQuery.getModifyTime();
            if (StringUtils.hasText(tagName)) {
                tagQueryWrapper.like("tag_name",tagName);
            } else if (ObjectUtil.isNotNull(begin)) {
                tagQueryWrapper.lt("create_time",begin);
            } else if (ObjectUtil.isNotNull(end)) {
                tagQueryWrapper.gt("create_time",end);
            }
        }
        Page<Tag> page = pageList(current, count, tagService, tagQueryWrapper);
        List<TagPostCount> list = BeanUtil.copyToList(page.getRecords(), TagPostCount.class);
        list.forEach(tag -> {
            tag.setCount(postTagService.count(new QueryWrapper<PostTag>().eq("tag_id", tag.getId())));
        });
        return Result.ok().data("total",page.getTotal()).data("pages",page.getPages()).data("current",page.getCurrent()).data("list",list);
    }

    @GetMapping("/del/{tagId}")
    public Result delTag (@PathVariable("tagId") String tagId) {
        postTagService.remove(new QueryWrapper<PostTag>().eq("tag_id",tagId));
        return tagService.removeById(tagId)?Result.ok():Result.fail();
    }

    @GetMapping("/getTagNames")
    public Result getTagNames () {
        return Result.ok("tagList",tagService.getTagList());
    }

    public Page<Tag> pageList(Integer current, Integer count, TagService service, QueryWrapper<Tag> wrapper) {
        Page<Tag> page = new Page<>(current,count);
        service.page(page,wrapper);
        return page;
    }
}
